package singleton;

/**
 * 双重校验锁的单例
 *
 * 目的: 解决线程安全带来的时间开销
 * 实现:
 * 1. 同步锁应当只在未创建实例时使用
 * 2. 未实例化使用synchronized
 * 
 * @author insight 
 * @since 2021/4/22
 */
public class SingletonDoubleCheck {
    private static SingletonDoubleCheck instance;

    private SingletonDoubleCheck() {
        System.out.println("SingletonDoubleCheck is Instantiated!");
    }

    public static SingletonDoubleCheck getInstance() {
        if (instance == null) {
            synchronized (SingletonDoubleCheck.class) {
                if (instance == null) {
                    instance = new SingletonDoubleCheck();
                }
            }
        }
        return instance;
    }
}